//
//  CollectionViewController.m
//  BGM
//
//  Created by liuqin on 2018/11/26.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "CollectionViewController.h"
#import "CollectionTableViewCell.h"
#import "Common.h"
#import "BGMHTTP.h"
#import "BGMUser.h"
#import "PushMessageInfoViewController.h"
#import "MJRefresh.h"
@interface CollectionViewController ()<UITableViewDelegate,UITableViewDataSource,CollectionTableViewCellDelegate>

@property (nonatomic, strong)UITableView *myTableView;
@property (nonatomic, strong)NSMutableArray *dataArray;

@property (nonatomic, assign)int currentPage;


@end

@implementation CollectionViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navview.hidden = NO;
    self.titleBtn.hidden = NO;
    [self.titleBtn setTitle:@"关注" forState:UIControlStateNormal];
    
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navview.frame.size.height, SCREENW, SCREENH-self.navview.frame.size.height)];
    self.myTableView.backgroundColor = [UIColor clearColor];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.dataArray = [[NSMutableArray alloc]init];
    self.currentPage = 1;
   

    
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
        self.currentPage = 1;
        [self requestFolled];
    }];
    
    self.myTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.currentPage==0) {
            [self.myTableView.mj_footer endRefreshing];
        }else{
            [self requestFolled];
        }
    }];
    
     [self.myTableView.mj_header beginRefreshing];
}
- (void)requestFolled{
    NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,@"userId":[BGMUser shareInstance].userId,@"pageNum":[NSString stringWithFormat:@"%d",self.currentPage],@"pageSize":@"10"};
    [BGMHTTP requestWithMethodName:@"index/getUsersFollowList" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
        NSArray *array = [result objectForKey:@"data"];
        [self getFollowData:array];
    }];
}

-(void)getFollowData:(NSArray *)array{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        [self.dataArray addObjectsFromArray:array];
        [self.myTableView reloadData];
        if (self.dataArray.count==0) {
            self.myTableView.hidden = YES;
            self.mormalLabel.hidden = NO;
        }else{
          
            if (array.count==10) {
                self.currentPage = self.currentPage +1;
            }else{
                self.currentPage = 0;
            }
            
       
            
        }
    });
}


#pragma mark UITABLEVIEWDELEAGE
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;//(SCREENH-70)/5+10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"CollectionTableViewCell";
    CollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[CollectionTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //添加长按手势
        UILongPressGestureRecognizer * longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(cellLongPress:)];
        longPressGesture.minimumPressDuration=0.3f;//设置长按 时间
        [cell addGestureRecognizer:longPressGesture];
        
        cell.collectionDelegate = self;
    }
    cell.deleBtn.tag = indexPath.row;
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
    [cell cellWithMessage:dic];
    return cell;
}
- (void)cellLongPress:(UILongPressGestureRecognizer *)longRecognizer{
    if (longRecognizer.state==UIGestureRecognizerStateBegan) {
       //可以得到此时你点击的哪一行
        CGPoint location = [longRecognizer locationInView:self.myTableView];
        NSIndexPath * indexPath = [self.myTableView indexPathForRowAtPoint:location];
        CollectionTableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
        cell.upView.hidden = NO;
        cell.deleBtn.hidden = NO;
        //在此添加你想要完成的功能
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [self.dataArray objectAtIndex:indexPath.row];
    PushMessageInfoViewController *vc = [[PushMessageInfoViewController alloc]init];
    NSString *type = [NSString stringWithFormat:@"%@",[dic objectForKey:@"publishTypeId"]];
    if([type intValue]>8){
        vc.isPublicMsg = YES;
    }
    vc.isMe = YES;
    vc.messageId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"publishId"]];
    vc.helpUsersId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"helpUsersId"]];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)deleCollection:(int)i{
    NSDictionary *dic = [self.dataArray objectAtIndex:i];
    [self.dataArray removeObjectAtIndex:i];
    [self.myTableView reloadData];
    
    [self notFollow:dic];
    
}
- (void)notFollow:(NSDictionary *)dic{
    NSDictionary *params = @{@"userToken":[BGMUser shareInstance].userToken,@"publishId":[NSString stringWithFormat:@"%@",[dic objectForKey:@"publishId"]],@"usersId":[BGMUser shareInstance].userId};
    
    [BGMHTTP requestWithMethodName:@"index/removeFollow" parameters:params completion:^(BOOL success, NSError *error, NSDictionary *result) {
        NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
        
        if ([code isEqualToString:@"200"]) {
            NSMutableArray *userResult = [[NSMutableArray alloc]init];
            NSMutableArray *publicResult = [[NSMutableArray alloc]init];

            for (NSDictionary *dic in self.dataArray) {
                NSString *typeID = [NSString stringWithFormat:@"%@",[dic objectForKey:@"publishTypeId"]];
                if ([typeID intValue]>8) {
                    [publicResult addObject:[dic objectForKey:@"publishId"]];
                }else{
                    [userResult addObject:[dic objectForKey:@"publishId"]];
                }
            }
            
            NSArray *array = @[@{@"type":@"1",@"dic":publicResult},@{@"type":@"0",@"dic":userResult}];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"HomeChangeInfo" object:array];
          
        }
        
        
        
    }];
    
}


@end
